const express = require('express');
const cors = require('cors');
const helmet = require('helmet');
const path = require('path');
const postsRouter = require('./routes/posts');
const { initDB } = require('./db/database');

const app = express();
const PORT = process.env.PORT || 3000;

// 初始化数据库
initDB();

// 中间件
app.use(helmet());
app.use(cors());
app.use(express.json());
app.use(express.static(path.join(__dirname, '../frontend')));

// API路由
app.use('/api/posts', postsRouter);

// 首页路由
app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname, '../frontend/index.html'));
});

// 启动服务器
app.listen(PORT, () => {
    console.log(`🌳 匿名树洞服务器运行在 http://localhost:${PORT}`);
    console.log(`📱 打开浏览器访问 http://localhost:${PORT} 开始使用`);
});
